<?php

namespace App\Modules\LuckDraw\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\FinanceServices;
use App\Http\Services\UserFilterServices;
use App\Http\Services\UserServices;
use App\Modules\LuckDraw\LuckDraw\LuckDrawFactory;
use App\Modules\LuckDraw\Services\LuckDrawService;
use App\Http\Model\System;
use App\Http\Services\CustomServices;
use App\Http\Services\MsgServices;

class LuckDrawController extends BaseController {
    protected $systemModel;
    protected $msgService;
    protected $customService;

    public function __construct(Request $request) {
        $this->systemModel = new System();
        $this->msgService = new MsgServices();
        $this->customService = new CustomServices();
    }

    //抽奖
    public function draw(Request $request){
        /**
         * 第一步，获取请求参数
         **/
        //获取活动的id,
        $luckId = (int) $request->get('luck_id', 0);
        //获取抽奖次数
        $drawNum = (int) $request->get('draw_num', 0);
        //获取房间id
        $roomId = (int) $request->get('room_id', 0);

        //检查参数
        if ($luckId<=0 || $drawNum<=0){
            Response::returnResult(Hint::PARAM_ERROR);
        }


        /**
         * 第二步，初始化抽奖
         **/
        //获取用户id，如果为空，返回未登录错误
        $user = (new UserServices())->get_current_user();

        if (empty($user)){
            Response::returnResult(Hint::LOGIN_OUT);
        }

        //获取抽奖活动实例
        $luckDrawInstance = LuckDrawFactory::get($luckId,  (object)$user, $roomId);
        
        if ($luckDrawInstance == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //检查用户是否可以进行抽奖
        list($checkCode, $checkMsg) = $luckDrawInstance->check();
        if ($checkCode !== Hint::SUCCESS) { //检查失败了：比如活动未开始，能量值不足，道具不足
            Response::returnResult($checkCode, [], $checkMsg);
        }

        /**
         * 第三步，执行抽奖
         **/
        $luckDrawService = new LuckDrawService($luckDrawInstance);
        $awards = $luckDrawService->run($drawNum);

        //返回
        if ($awards == null) {
            Response::returnResult(Hint::ERR_NETWORK_BUSY);
        }

        //人气票发放
        $this->customService->SendTicket($user['user_id'], $drawNum, $luckDrawService->price(), 7);

        //房间内游戏公屏游戏消息
        if($roomId > 10) {
            //查询飘屏中设置的飘屏限制钻石数,因为公屏限制和飘屏限制一致
            $fullScreen = $this->systemModel->getFullScreen(22);
            if (empty($fullScreen)) {
                return false;
            }
            $awards = json_decode(json_encode($awards), true);
            foreach($awards as $val) {
                if($val['price'] >= $fullScreen['special_full_screen_diamond']) {
                    $this->msgService->roomWishPoolMsg($roomId, ['nickname'=>$user['nickname'], 'user_id'=>$user['user_id'], 'placeName'=>'许愿池', 'giftName'=>$val['name'], 'giftPrice'=>$val['price'], 'giftNum'=>$val['num']]);
                }
            }
        }

        Response::returnResult(Hint::SUCCESS, $awards);
    }


    //兑换抽奖道具
    public function exchange(Request $request) {
        $luckId = $request->get('luck_id', 0);
        $num = $request->get('num', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($num) || $num <= 0 || !is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户
        $user = (new UserServices())->get_current_user();
        $userId = $user['user_id'] ?? 0;

        //没有登录，返回未登录错误
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        // $isAdult = UserVerifyServices::isAdult($userId);
        // if ($isAdult == 0) {
        //     Response::returnResult(Hint::JUVENILES_NOT_REWARD);
        // }
        // if ($isAdult == -1) {
        //     Response::returnResult(Hint::ROOM_GIFT__USER_NOT_VERIFY);
        // }

        //判断用户是否在禁止消费名单 add by zxiaofee
        if((new UserFilterServices)->inConsumeUserBlacklist($userId)){
            Response::returnResult(Hint::ROOM_GIFT_USER_CONSUME_FORBID);
        }
        //添加消费限制
        if((new FinanceServices())->reachConsumeToplimit($userId)){
            Response::returnResult(Hint::FINANCE_CONSUME_TOPLIMIT);
        }

        //获取一个抽奖活动实例
        $luckDrawInstance = LuckDrawFactory::get($luckId, (object)$user);
        if ($luckDrawInstance == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //检查经验值是否达到要求
        if (!$luckDrawInstance->expEnough()) {
            Response::returnResult(Hint::LUCK_DRAW_EXP_NOT_ENOUGH, [], $luckDrawInstance->getExpName() . '达到' . $luckDrawInstance->getExpNeed() . '，才能开启');
        }

        //开始兑换
        $luckDrawService = new LuckDrawService($luckDrawInstance);
        list($code, $data) = $luckDrawService->exchange($num);

        //返回兑换结果
        Response::returnResult($code, $data??[]);
    }

    //活动总送抽奖道具
    public function present(Request $request) {
        $luckId = $request->get('luck_id', 0);
        $num = $request->get('num', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($num) || $num <= 0 || !is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户
        $user = (new UserServices())->get_current_user();
        $userId = $user['user_id'] ?? 0;

        //没有登录，返回未登录错误
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        //获取一个抽奖活动实例
        $luckDrawInstance = LuckDrawFactory::get($luckId, (object)$user);
        if ($luckDrawInstance == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //开始兑换
        $luckDrawService = new LuckDrawService($luckDrawInstance);
        list($code, $data) = $luckDrawService->present($num);

        //返回兑换结果
        Response::returnResult($code, $data??[]);
    }

    //获取当前用户当前活动下的资产信息
    public function balance(Request $request) {
        $luckId = $request->get('luck_id', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户
        $user = (new UserServices())->get_current_user();

        //没有登录，返回未登录错误
        if (empty($user)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        //获取一个抽奖活动实例 //检查活动是否存在 //不需要room 传0就行
        $luckDraw = LuckDrawFactory::get($luckId, (object)$user);
        if ($luckDraw == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户当前活动下的资产信息
        $luckDrawService = new LuckDrawService($luckDraw);
        $balance = $luckDrawService->banlance();

        Response::returnResult(Hint::SUCCESS, $balance);
    }

    //获取抽奖页面详情
    public function rule(Request $request) {
        $luckId = $request->get('luck_id', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取一个抽奖活动实例 //检查活动是否存在
        $luckDraw = LuckDrawFactory::get($luckId);
        if ($luckDraw == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前活动规则
        $luckDrawService = new LuckDrawService($luckDraw);
        $balance = $luckDrawService->rule();

        Response::returnResult(Hint::SUCCESS, $balance);
    }

    //中奖记录
    public function record(Request $request) {
        //获取参数
        $luckId = $request->get('luck_id', 0);
        $pageNum = $request->get('page_num', 0);// 页数
        $pageSize = $request->get('page_size', 20);// 条数
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0
            || !is_numeric($pageNum) || $pageNum <0
            || !is_numeric($pageSize) || $pageSize <=0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //安卓需要处理一下
        $parse_app_UA = parse_app_UA();
        if(!empty($ua['os_name']) && $ua['os_name']=='Android') {
            $pageNum = $pageNum + 1;
        }
        //获取当前用户
        $user = (new UserServices())->get_current_user();
        $userId = $user['user_id'] ?? 0;

        //没有登录，返回未登录错误
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        //获取一个抽奖用户的实例
        $luckDraw = LuckDrawFactory::get($luckId,(object)$user);

        //获取当前用户当前活动下的资产信息
        $luckDrawService = new LuckDrawService($luckDraw);
        $record = $luckDrawService->record($pageNum, $pageSize);

        Response::returnResult(Hint::SUCCESS, $record);
    }

    //排行榜
    public function ranking(Request $request) {
        $luckId = $request->get('luck_id', 0);
        $top = $request->get('top', 50);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0
            || !is_numeric($top) || $top <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取一个抽奖活动实例 //检查活动是否存在
        $luckDraw = LuckDrawFactory::get($luckId);
        if ($luckDraw == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //获取当前用户当前活动下的资产信息
        $luckDrawService = new LuckDrawService($luckDraw);
        $ranking = $luckDrawService->ranking($top);
        if ($ranking === null) {
            Response::returnResult(Hint::ERR_SYS_FAIL);
        }
        Response::returnResult(Hint::SUCCESS, $ranking);
    }

    public function price(Request $request) {
        $luckId = $request->get('luck_id', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取一个抽奖活动实例 //检查活动是否存在
        $luckDraw = LuckDrawFactory::get($luckId);
        if ($luckDraw == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前活动单次价格
        $luckDrawService = new LuckDrawService($luckDraw);
        $price = $luckDrawService->price();
        Response::returnResult(Hint::SUCCESS, ['price'=>$price]);
    }

    public function isLocked(Request $request){
        $luckIds = $request->get('luck_ids', '');
        //验证参数
        if (empty($luckIds)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $luckIds = explode(',', $luckIds);
        if (empty($luckIds)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $lockedArr = (new LuckDrawService())->isLocked($luckIds);
        if ($lockedArr == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        Response::returnResult(Hint::SUCCESS, $lockedArr);
    }

    public function awardList(Request $request){
        $luckId = $request->get('luck_id', 0);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取用户id，如果为空，返回未登录错误
        $user = (new UserServices())->get_current_user();
        if (empty($user) || empty($user['user_id'])){
            Response::returnResult(Hint::LOGIN_OUT);
        }

        $luckDrawInstance = LuckDrawFactory::get($luckId,(object)$user);
        if ($luckDrawInstance == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户奖池数据
        $luckDrawService = new LuckDrawService($luckDrawInstance);
        $awards = $luckDrawService->awardList();
        //返回
        if ($awards == null) {
            Response::returnResult(Hint::ERR_NETWORK_BUSY);
        }
        Response::returnResult(Hint::SUCCESS, $awards);
    }

    public function marquee(Request $request){
        $luckId = $request->get('luck_id', 0);
        $top = $request->get('top', 40);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($luckId) || $luckId <= 0
            || !is_numeric($top) || $top <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取一个抽奖活动实例 //检查活动是否存在
        $luckDraw = LuckDrawFactory::get($luckId);
        if ($luckDraw == null) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户当前活动最新中奖记录
        $luckDrawService = new LuckDrawService($luckDraw);
        $ranking = $luckDrawService->marquee($top);
        if ($ranking === null) {
            Response::returnResult(Hint::ERR_SYS_FAIL);
        }
        Response::returnResult(Hint::SUCCESS, $ranking);
    }
}